package itstudio.instructor.adapter;

import java.util.ArrayList;
import java.util.List;
import itstudio.instructor.entity.BorrowBook;
import itstudio.instructor.util.FileUtils;
import com.easemob.chatuidemo.R;
import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class LibraryListAdapter extends BaseAdapter {

	private LayoutInflater mInflater;
	private List<BorrowBook> bookList =new ArrayList<BorrowBook>();
	private Context context;
	
	public LibraryListAdapter(Context context,  List<BorrowBook> newsList) {
		if(newsList!=null){
			this.bookList = newsList;
		}
		this.context = context;
		this.mInflater = LayoutInflater.from(context);
		
	}

	@Override
	public int getCount() {
		return bookList==null ? 0:bookList.size() ;
	}

	@Override
	public Object getItem(int position) {
		return bookList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public void appendToList(List<BorrowBook> addList) {

		if (addList == null) {
			return;
		}
		if (bookList == null) {
			bookList = new ArrayList<BorrowBook>();
		}
		bookList.addAll(addList);
		notifyDataSetChanged();
	}
	
	public void clearAll() {
	    bookList.clear();
	    notifyDataSetChanged();
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		PicModelHodler holder ;
		BorrowBook book = bookList.get(position);
		if(convertView == null){
			holder = new PicModelHodler();
			convertView = mInflater.inflate(R.layout.listitem_book, parent,false);
			holder.imageView = (ImageView) convertView.findViewById(R.id.book_image);
			holder.author = (TextView) convertView.findViewById(R.id.author);
			holder.bookname = (TextView) convertView.findViewById(R.id.bookname);
			holder.start_date = (TextView) convertView.findViewById(R.id.start_date);
			holder.left_date = (TextView) convertView.findViewById(R.id.left_date);
			holder.play = (Button) convertView.findViewById(R.id.play);
			convertView.setTag(holder);
		}
		else{
			holder = (PicModelHodler) convertView.getTag();
		}
		FileUtils.setLoacalImg(book.getPicuri(), holder.imageView);
		holder.author.setText(book.getAuthor());
		holder.bookname.setText(book.getName());
		holder.start_date.setText(book.getStartDate());
		holder.left_date.setText(book.getLeftDate());
		holder.left_date.setText(book.getLeftDate());
		final TextView playTv =holder.play;
		if(book.getState()==BorrowBook.OUT_OF_DATE){
			holder.play.setBackgroundResource(R.drawable.button_red_selector);
			holder.play.setText("超期  请归还");
		}else if (book.getState()==BorrowBook.NORMAL){
			holder.play.setBackgroundResource(R.drawable.button_green_selector);
			holder.play.setText("续借");
			if("续借".equals(holder.play.getText().toString())){
				holder.play.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						playTv.setText("续借中...");
						playTv.setClickable(false);
						new Handler().postDelayed(new Runnable() {
							
							@Override
							public void run() {
								playTv.setText("已续借");
								playTv.setBackgroundResource(R.drawable.button_gray_selector);
							}
						}, 1000);
					}
				});
			}
			
		}else{
			holder.play.setBackgroundResource(R.drawable.button_gray_selector);
			holder.play.setText("已续借");
			
		}
		return convertView;
	}

	private final class PicModelHodler {
		public ImageView imageView;
		public TextView author;
		public TextView bookname;
		public TextView start_date;
		public TextView left_date;
		public Button play;
	}
}
